class ManageShopController < ManageController
  before_filter:has_shop,
    :except=>[:shop_new,
              :shop_create]
  
  before_filter:check_product_owner,
    :only=>[:product_show,
            :product_update,
            :product_destroy,
            :product_edit
  ]
  before_filter:post?,
                 :only=>[:product_create,
                         :product_update,
                         :product_destroy,
                         :shop_create,
                         :shop_update]
                       
  def has_shop
    @shop=get_shop
    unless @shop
      redirect_to_shop_new
    end
  end
  
  def check_product_owner
    begin
      @product=Product.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      notice("商品不存在！")
      redirect_to(:controller=>"manage",:action=>:error_msg)
    else
      unless @product.shop.user.id==session[:user_id]
        notice("你没有权限操作该产品！")
        redirect_to(:controller=>"manage",:action=>:error_msg)
      end
    end
  end
  
  def post?
    unless request.post?
      notice("非正常操作！")
      redirect_to(:controller=>:manage,:action=>:error_msg)
    end
  end
  
  # Get manage_shop/shop_show
  def shop_show
  end

  def shop_edit
  end
  
  # Post manage_shop/shop_update
  def shop_update
      begin
        @shop.update_attributes!(params[:shop])
      rescue ActiveRecord::RecordNotSaved
        redirect_to_error_msg("商铺信息更新失败！")        
      else
        notice("商铺信息更新成功！")
        redirect_to(:action=>:shop_show)
      end
    end
  
  def shop_new
  end
  
  # Post manage_shop/shop_create
  def shop_create
      @shop=Shop.new(params[:shop])
      @shop.user_id=session[:user_id]
      @shop.status="open"
      begin
        @shop.save!
      rescue ActiveRecord::RecordNotSaved
        redirect_to_error_msg("开通商铺失败！")
      else
        notice("开通商铺成功！")
        redirect_to(:action=>:shop_show)
      end   
  end
  
  def shop_order_list
    @orders=Order.find(:all,:conditions=>["shop_id=?",@shop.id])
  end
  
  def shop_order_show
    @order = Order.find(params[:id])
    unless @order&&@order.shop_id==@shop.id
      notice("你无权限查看该订单！")
      redirect_to(:action=>:shop_order_list)
    else
      @items=LineItem.find(:all,:conditions=>["order_id=?",@order.id])
    end
  end
  
  def shop_order_ps
    @order = Order.find(params[:id])
    unless @order&&@order.shop_id==@shop.id
      notice("你无权限查看该订单！")
      redirect_to(:action=>:shop_order_list)
    else
      if OrderManager.product_sent(@order)
        notice("成功发货！")
      else
        notice("发货失败！")
      end
      redirect_to(:action=>:shop_order_show,:id=>@order.id)
    end
  end

  def shop_account_list
  end

  def shop_account_show
  end

  def product_show
  end

  def product_list
    @products=Product.find(:all,:conditions=>["shop_id=?",@shop.id])
  end

  def product_new
  end
  
  def product_create
      @product = Product.new(params[:product])
      @product.shop=@shop
      begin
        @product.save!
      rescue ActiveRecord::RecordNotSaved
        logger.error("product #{@product.title} created error")
        redirect_to_error_msg("创建失败！")
      else
        notice("创建成功！")
        redirect_to(:action=>:product_show,:id=>@product.id)
      end    
  end
  
  def product_edit
  end
  
  def product_update
    begin 
      @product.update_attributes!(params[:product])
    rescue ActiveRecord::RecordNotSaved
      logger.error("product #{@product.title} updated error")
      redirect_to_error_msg("更新失败!")
    else
      notice("更新成功！")
      redirect_to(:action=>:product_show,:id=>@product.id)
    end     
  end
  
  def product_destroy
    begin
    @product.destroy
    rescue ActiveRecord::ActiveRecordError
      logger.error("product #{@product.title} deleted error")
      redirect_to_error_msg("删除失败!")
    else
      notice("删除成功！")
      redirect_to(:action=>:product_list)
    end 
  end
  
  private
  def get_shop
    shop=Shop.find_by_user_id(session[:user_id])
    shop
  end

  def redirect_to_shop_new(msg="请先开通商铺！")
    notice(msg)
    redirect_to(:action=>:shop_new)
  end
  
  def redirect_to_error_msg(msg="发生错误！")
    notice(msg)
    redirect_to(:controller=>:manage,:action=>:error_msg)
  end
end
